package com.flow.asyncflow.core.entity.autooperation;

import com.flow.asyncflow.core.enums.AutoOperationEvent;
import com.flow.asyncflow.core.enums.AutoOperationEventState;
import com.flow.asyncflow.core.enums.PageNumberEnum;
import com.flow.asyncflow.core.utils.http.HttpClientRequest;
import com.flow.asyncflow.core.utils.http.HttpClientResponse;
import lombok.Data;

import java.util.List;

/**
 * 自动操作节点
 */
@Data
public class AutoOperationNode {
    /**
     * 元素名称【元素唯一号】,或者HTTP请求的说明
     */
    private String name;
    /**
     * 所在页码
     */
    private PageNumberEnum pageNumberEnum;
    /**
     * 页面所在URL
     */
    private String pageURL;
    /**
     * 执行事件的枚举
     */
    private AutoOperationEvent event;

    /**
     * 执行事件状态
     */
    private AutoOperationEventState eventState = AutoOperationEventState.UnExecuted;

    /**
     * 页面元素XPath、页面元素Selector http，请求地址
     */
    private String path;

    /**
     * 文本框或者下拉选择填充的内容，或者执行的JS内容
     */
    private String content;

    /**
     * 强制等待时间【毫秒】
     */
    private long waitTime;

    /**
     * 发送HTTP请求参数
     */
    private HttpClientRequest httpClientRequest;
    /**
     * HTTP返回参数
     */
    private HttpClientResponse httpClientResponse;
    /**
     * 执行耗时
     */
    private long timeCost;

    public static Builder builder() {
        return new Builder();
    }

    @Data
    public static class Builder {

        /**
         * 元素名称【元素唯一号】
         */
        private String name;
        /**
         * 所在页码
         */
        private PageNumberEnum pageNumberEnum;
        /**
         * 页面所在URL
         */
        private String pageURL;
        /**
         * 执行事件的枚举
         */
        private AutoOperationEvent event;
        /**
         * 页面元素XPath、页面元素Selector
         */
        private String path;
        /**
         * 文本框或者下拉选择填充的内容，或者执行的JS内容、或者http的请求参数
         */
        private String content;
        /**
         * 强制等待时间【毫秒】
         */
        private long waitTime;


        public Builder name(String elementName) {
            this.name = elementName;
            return this;
        }

        public Builder pageNum(PageNumberEnum pageNumberEnum) {
            this.pageNumberEnum = pageNumberEnum;
            return this;
        }

        public Builder event(AutoOperationEvent event) {
            this.event = event;
            return this;
        }

        public Builder path(String path) {
            this.path = path;
            return this;
        }

        public Builder content(String content) {
            this.content = content;
            return this;
        }

        public Builder pageURL(String pageURL) {
            this.pageURL = pageURL;
            return this;
        }


        public Builder waitTime(long waitTime) {
            this.waitTime = waitTime;
            return this;
        }


        public AutoOperationNode build(List<AutoOperationNode> autoOperationNodeList) {
            AutoOperationNode autoOperationNode = new AutoOperationNode();
            autoOperationNode.setName(this.name);
            autoOperationNode.setPageNumberEnum(this.pageNumberEnum);
            autoOperationNode.setPageURL(this.pageURL);
            autoOperationNode.setEvent(this.event);
            autoOperationNode.setPath(this.path);
            autoOperationNode.setContent(this.content);
            autoOperationNode.setWaitTime(this.waitTime);
            autoOperationNodeList.add(autoOperationNode);
            return autoOperationNode;
        }


        /**
         * 构建返回结果
         *
         * @param event    事件类型
         * @param path     元素页面路径
         * @param content  元素内容
         * @param waitTime 强制等待时间
         * @return 元素
         */
        public AutoOperationNode build(AutoOperationEvent event, String path, String content, long waitTime) {
            AutoOperationNode autoOperationNode = new AutoOperationNode();
            autoOperationNode.setName(path);
            autoOperationNode.setPageNumberEnum(this.pageNumberEnum);
            autoOperationNode.setEvent(event);
            autoOperationNode.setPath(path);
            autoOperationNode.setContent(content);
            autoOperationNode.setWaitTime(waitTime);
            return autoOperationNode;
        }

        public AutoOperationNode build() {
            AutoOperationNode autoOperationNode = new AutoOperationNode();
            autoOperationNode.setName(this.name);
            autoOperationNode.setPageNumberEnum(this.pageNumberEnum);
            autoOperationNode.setPageURL(this.pageURL);
            autoOperationNode.setEvent(this.event);
            autoOperationNode.setPath(this.path);
            autoOperationNode.setContent(this.content);
            autoOperationNode.setWaitTime(this.waitTime);
            return autoOperationNode;
        }
    }

}
